package com.frogs.ucenter.component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.frogs.framework.component.cache.Cache;
import com.frogs.framework.util.StringUtil;
import com.frogs.system.util.GoodsEvaluateEnum;
import com.frogs.system.util.UserTypeEnum;
import com.frogs.ucenter.dao.GoodsEvaluateScoreDao;
import com.frogs.ucenter.dao.TagEvaluateRelationDao;
import com.frogs.ucenter.dao.UserLevelInfoDao;
import com.frogs.ucenter.entity.GoodsEvaluateScore;
import com.frogs.ucenter.entity.TagEvaluateRelation;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.util.DataCacheKey;

/**
 * 
 * @author hongyuhao
 *
 */
@Component("evaluateAsyncHelper")
public class EvaluateAsyncHelper extends RequestApiHelper {
	
	@Resource(name="xmemcached")
	private Cache cache;
	@Resource
	private GoodsEvaluateScoreDao goodsEvaluateScoreDao;
	@Resource
    private TagEvaluateRelationDao tagEvaluateRelationDao;
	@Resource
	private UserLevelInfoDao userLevelInfoDao;
    
	
	@Async
	public void cleanEvaluateCache(Integer userId , List<Integer> goodsIds ){
		this.cleanEvaluateCacheByUserId(userId);
		this.cleanEvaluateCacheByGoodsIds(goodsIds);
	}
	
	
	private void cleanEvaluateCacheByUserId(Integer userId) {
		StringBuilder mapKey = new StringBuilder();
		
		List<Integer> sortList = GoodsEvaluateEnum.getSortList();
		for (Integer id : sortList) {
			mapKey.setLength(0);
			mapKey.append(DataCacheKey.goodsEvaluateMapKey);
			mapKey.append("_USER_").append(userId);
			mapKey.append("_SORT_").append(id);
			cache.remove(mapKey.toString());
		}
		
	}
	
	private void cleanEvaluateCacheByGoodsIds(List<Integer> goodsIds ){
		StringBuilder mapKey = new StringBuilder();
		if (goodsIds != null) {
			for (int i=0; i<goodsIds.size(); i++) {
			    
				List<Integer> sortList = GoodsEvaluateEnum.getSortList();
				for (Integer id : sortList) {
					mapKey.setLength(0);
					mapKey.append(DataCacheKey.goodsEvaluateMapKey);
					mapKey.append("_GOODS_").append(goodsIds.get(i));
					mapKey.append("_SORT_").append(id);
					cache.remove(mapKey.toString());
				}
			}
		}
	}
	
	@Async
	public void creatEvaluateTagRelation(String ids, Integer userId, Integer evaluateId) {
		if (StringUtil.isEmpty(ids)) 
			return ;
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("eqUserId", userId);
		params.put("eqEvaluateId", evaluateId);
		int count = tagEvaluateRelationDao.selectCountByMap(params);
		String[] idArray = ids.split(",");
		if (count > 0) {
			tagEvaluateRelationDao.deleteByMap(params);
		}
		for (int i=0; i<idArray.length; i++) {
			TagEvaluateRelation tagEvaluateRelation = new TagEvaluateRelation();
			tagEvaluateRelation.setTagId(Integer.valueOf(idArray[i]));
			tagEvaluateRelation.setEvaluateId(evaluateId);
			tagEvaluateRelation.setUserId(userId);
			tagEvaluateRelationDao.insert(tagEvaluateRelation);
		}
	}
	
	@Async
	public void insertGoodsEvaluateScore(int userId, int goodsId,int mealId, int score) {
		if (score < 0) 
			return ;
		
		//根据商品id获取同宗商品ids
		List<Integer> ids = getGoodsSimilarList(goodsId);
		UserLevelInfo userLevelInfo = userLevelInfoDao.selectById(userId);
		Integer levelId = userLevelInfo.getLevelId();
		if (userLevelInfo.getDynamicVipLevelId() != null) {
			if (userLevelInfo.getDynamicVipDeadline().getTime() > new Date().getTime()) {
				levelId = userLevelInfo.getDynamicVipLevelId();
			}
		}
		int weight = 0;
		if (levelId == null || levelId == 0) {
			weight = UserTypeEnum.getByType(UserTypeEnum.GENERAL.getType()).getWeight();
		} else {
			weight = UserTypeEnum.getByType(levelId).getWeight();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("eqUserId", userId);
		params.put("inGoodsIdList", ids);
		int count = goodsEvaluateScoreDao.selectCountByMap(params);
		if (count > 0) {
			for (int i=0; i<ids.size(); i++) {
				params.clear();
				params.put("eqUserId", userId);
				params.put("eqGoodsId", ids.get(i));
				params.put("score", score);
				params.put("weight", weight);
				goodsEvaluateScoreDao.updateByMap(params);
			}
		} else {
			for (int i=0; i<ids.size(); i++) {
				GoodsEvaluateScore goodsEvaluateScore = new GoodsEvaluateScore();
				goodsEvaluateScore.setGoodsId(ids.get(i));
				goodsEvaluateScore.setScore(score);
				goodsEvaluateScore.setUserId(userId);
				goodsEvaluateScore.setWeight(weight);
				goodsEvaluateScoreDao.insert(goodsEvaluateScore);
			}
		}
	}
}
